﻿using System.Diagnostics;

namespace RealidadeAumentada.Entidades
{
    public class EPilhaPonteiro<T>
    {
        protected T[] _items;
        protected int _length;

        #region Construtor
        protected EPilhaPonteiro()
        {
        }
        #endregion

        #region Obter Instancia
        protected virtual void ObterInstancia(int i_length)
        {
            this._items = new T[i_length];
            this._length = 0;
            return;
        }
        #endregion

        #region Forçar
        public virtual T Forcar(T i_object)
        {
            if (this._length >= this._items.Length)
            {
                return default(T);
            }
            this._items[this._length] = i_object;
            this._length++;
            return i_object;
        }
        #endregion

        #region Declarar
        public T Declarar(T i_object)
        {
            Debug.Assert(this._length < this._items.Length);
            this._items[this._length] = i_object;
            this._length++;
            return i_object;
        }
        #endregion

        #region Estouro
        public virtual T Estouro()
        {
            Debug.Assert(this._length >= 1);
            this._length--;
            return this._items[this._length];
        }

        public virtual void Estouros(int i_count)
        {
            Debug.Assert(this._length >= i_count);
            this._length -= i_count;
            return;
        }
        #endregion

        #region Obter Array
        public T[] ObterArray()
        {
            return this._items;
        }
        #endregion

        #region Obter Item
        public T ObterItem(int i_index)
        {
            return this._items[i_index];
        }
        #endregion

        #region Obter Tamanho
        public int ObterTamanho()
        {
            return this._length;
        }
        #endregion

        #region Remover
        public virtual void Remover(int i_index)
        {
            Debug.Assert(this._length > i_index && i_index >= 0);

            if (i_index != this._length - 1)
            {
                int i;
                int len = this._length - 1;
                T[] items = this._items;
                for (i = i_index; i < len; i++)
                {
                    items[i] = items[i + 1];
                }
            }
            this._length--;
        }
        #endregion

        #region Remover da Lista de Ignorados
        public virtual void RemoverOrdemIgnorar(int i_index)
        {
            Debug.Assert(this._length > i_index && i_index >= 0);
            if (i_index != this._length - 1)
            {
                this._items[i_index] = this._items[this._length - 1];
            }
            this._length--;
        }
        #endregion

        #region Limpar
        public void Limpar()
        {
            this._length = 0;
        }
        #endregion
    }
}
